//
//  BoundAlipayController.m
//  ShrimpCrowdordering
//
//  Created by 骆锴 on 2018/9/14.
//  Copyright © 2018年 郑洲. All rights reserved.
//

#import "BoundAlipayController.h"

@interface BoundAlipayController ()

@property (nonatomic, strong) UITextField *aliPayNumber;
@property (nonatomic, strong) UIButton *sureBtn;

@end

@implementation BoundAlipayController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = REXADECIMALCOLOR(@"#eeeeee");
    // Do any additional setup after loading the view.
    self.title = @"支付宝绑定";

    [self setUI];
}

- (void)viewWillDisappear:(BOOL)animated{
    self.result(_isAliPay,[_aliPayNum copy]);
}

- (void)setUI {
    UIView *baseView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, ScreenX375(47))];
    baseView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:baseView];
    
    UILabel *titleLabel =[[UILabel alloc] initWithFrame:CGRectMake(ScreenX375(16), ScreenX375(16), ScreenX375(100), ScreenX375(15))];
    titleLabel.text = @"支付宝账号";
    titleLabel.font = Kfont(15);
    titleLabel.textColor = REXADECIMALCOLOR(@"#333333");
    titleLabel.textAlignment = NSTextAlignmentLeft;
    [baseView addSubview:titleLabel];
    
    _aliPayNumber =[[UITextField alloc] initWithFrame:CGRectMake(ScreenX375(209), ScreenX375(16), ScreenX375(150), ScreenX375(15))];
    _aliPayNumber.placeholder = @"请输入支付宝账号";
    _aliPayNumber.font = Kfont(15);
    _aliPayNumber.textColor = REXADECIMALCOLOR(@"#333333");
    _aliPayNumber.textAlignment = NSTextAlignmentRight;
    [baseView addSubview:_aliPayNumber];
    
    _sureBtn = [[UIButton alloc] initWithFrame:CGRectMake(ScreenX375(16),ScreenX375(87), ScreenX375(343), ScreenX375(49))];
    [_sureBtn setBackgroundImage:[UIImage imageNamed:@"按钮背景"] forState:UIControlStateNormal];
    [_sureBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    _sureBtn.titleLabel.font = Kfont(16);
    _sureBtn.layer.cornerRadius = ScreenX375(3);
    _sureBtn.clipsToBounds = YES;
    [self.view addSubview:_sureBtn];
    
    [self setBtn];
    [self setTextField];
}

-(void)relieveAliPay{
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"是否解除绑定" preferredStyle:UIAlertControllerStyleAlert];
    
    [alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
    }]];

    [alertController addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            NSString *AliPay = [[NSString alloc] init];
            AliPay = @"-1";
            NSDictionary *parameter = @{@"id":kNSUDefaultReadKey(@"userId"),@"aliAccount":AliPay};
            [HttpManager postHttpRequestByPost:[NSString stringWithFormat:@"%@api/updateUserInfo",REQUESTHEADER] andParameter:parameter success:^(id successResponse) {
                NSString *code = [NSString stringWithFormat:@"%@",successResponse[@"code"]];
                if ([code isEqualToString:@"200"]) {
                    [HttpManager showNoteMsg:@"解除成功"];
                    self.isAliPay = NO;
                    [self setTextField];
                    [self setBtn];
                }else {
                    [HttpManager showNoteMsg:[NSString stringWithFormat:@"%@",successResponse[@"message"]]];
                }
            } andFailure:^(id failureResponse) {
                [HttpManager showFail];
            }];
    }]];
    [self presentViewController:alertController animated:YES completion:nil];
}

-(void)setBtn{
    if (_isAliPay == YES) {
        [_sureBtn setTitle:@"解绑" forState:UIControlStateNormal];
        [_sureBtn removeTarget:self action:@selector(boundAlipay) forControlEvents:UIControlEventTouchUpInside];
        [_sureBtn addTarget:self action:@selector(relieveAliPay) forControlEvents:UIControlEventTouchUpInside];
    }else{
        [_sureBtn setTitle:@"绑定" forState:UIControlStateNormal];
        [_sureBtn removeTarget:self action:@selector(relieveAliPay) forControlEvents:UIControlEventTouchUpInside];
        [_sureBtn addTarget:self action:@selector(boundAlipay) forControlEvents:UIControlEventTouchUpInside];
    }
}

-(void)setTextField{
    if (_isAliPay == YES) {
        _aliPayNumber.enabled = NO;
        _aliPayNumber.text = [_aliPayNum copy];
    }else{
        _aliPayNumber.enabled = YES;
        _aliPayNumber.text = nil;
    }
}

-(void)boundAlipay{
    if (self.aliPayNumber.text.length == 0) {
        [HttpManager showNoteMsg:@"请输入支付宝账号"];
    }else{
        NSDictionary *parameter = @{@"id":kNSUDefaultReadKey(@"userId"),@"aliAccount":_aliPayNumber.text};
        [HttpManager postHttpRequestByPost:[NSString stringWithFormat:@"%@api/updateUserInfo",REQUESTHEADER] andParameter:parameter success:^(id successResponse) {
            NSString *code = [NSString stringWithFormat:@"%@",successResponse[@"code"]];
            if ([code isEqualToString:@"200"]) {
                [HttpManager showNoteMsg:@"绑定成功"];
                self.aliPayNum = [self.aliPayNumber.text mutableCopy];
                self.isAliPay = YES;
                [self setBtn];
                [self setTextField];
            }else {
                [HttpManager showNoteMsg:[NSString stringWithFormat:@"%@",successResponse[@"message"]]];
            }
        } andFailure:^(id failureResponse) {
            [HttpManager showFail];
        }];
    }
}

@end
